package com.android.delivery.dialog;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.android.delivery.R;
import com.android.delivery.constant.Constants;
import com.android.delivery.entity.Scan;


/**
 * Desc:扫描成功弹框
 * <p>
 * Author: [李豫]
 * Date: 2018-08-02
 * Copyright: Copyright (c) 2010-2018
 * Updater:
 * Update Time:
 * Update Comments:
 */
public class SanSuccDialog extends DialogFragment implements View.OnClickListener {

    private Scan mScan;
    private TextView mTvOrderNo;
    private TextView mTvType;
    private TextView mTvStartNode;
    private TextView mTvReceNode;

    public static SanSuccDialog newInstance(Scan scan) {
        SanSuccDialog payMethodDialog = new SanSuccDialog();
        Bundle bundle = new Bundle();
        bundle.putParcelable(Constants.ITEM, scan);
        payMethodDialog.setArguments(bundle);
        return payMethodDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(true);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialog);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setCanceledOnTouchOutside(true);
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.CENTER);
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View dialogView = inflater.inflate(R.layout.dialog_scan_succ, container, false);

        mTvOrderNo = dialogView.findViewById(R.id.tv_no);
        mTvType = dialogView.findViewById(R.id.tv_type);
        mTvStartNode = dialogView.findViewById(R.id.tv_start_node);
        mTvReceNode = dialogView.findViewById(R.id.tv_receiving_node);

        dialogView.findViewById(R.id.iv_close).setOnClickListener(this);

        initData();
        return dialogView;
    }

    private void initData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mScan = bundle.getParcelable(Constants.ITEM);
            if (mScan != null) {
                mTvOrderNo.setText(mScan.getLogisticsNo());
                mTvType.setText(String.valueOf(mScan.getLogisticsType()));
                mTvStartNode.setText(mScan.getSourceLocationName());
                mTvReceNode.setText(mScan.getTargetLocationName());
            }
        }
    }

    public void show(FragmentManager manager) {
        if (!isShowing()) {
            show(manager, SanSuccDialog.class.getSimpleName());
        }
    }

    public boolean isShowing() {
        if (getDialog() != null && getDialog().isShowing()) {
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        if (viewId == R.id.iv_close) {
            dismiss();
        }
    }
}

